package com.desmart.renjiao.announcement;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.desmart.renjiao.R;
import com.desmart.renjiao.bean.Announcement;
import com.lemon.library.common.ARoutePath;
import com.lemon.library.common.CommonActivity;
import com.lemon.library.common.router.Contanst;
import com.lemon.library.common.router.JumpUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

@Route(path = ARoutePath.home.AnnouncementListActivity)
public class AnnouncementListActivity extends CommonActivity<AnnouncementListM,AnnouncementListP> implements AnnouncementList.AnnouncementListV{

    private SmartRefreshLayout refreshLayout;
    private RecyclerView recyclerview;
    private String loginName;
    private List<Announcement> mList = new ArrayList<>();
    private int pageNum = 1;
    private AnnouncementAdapter adapter;

    @Override
    public int bindLayout() {
        return R.layout.activity_announcement_list;
    }

    @Override
    public void initView(Activity activity) {

        refreshLayout = activity.findViewById(R.id.refreshlayout);
        recyclerview = activity.findViewById(R.id.recyclerview);

        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setEnableLoadMore(true);

        recyclerview.setLayoutManager(new LinearLayoutManager(this));

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                mPresenter.getAnnouncement(pageNum,loginName);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNum++;
                mPresenter.getAnnouncement(pageNum,loginName);
            }
        });

        adapter = new AnnouncementAdapter(mList);
        recyclerview.setAdapter(adapter);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Announcement announcement = mList.get(position);
                JumpUtils.toWeb(AnnouncementListActivity.this,announcement.link,announcement.title);
            }
        });
    }

    @Override
    public void initData(Context context) {

        String type = getIntent().getStringExtra(Contanst.annnouncement.Key_Type);
        if (Contanst.annnouncement.type_ben.equals(type)){
            setTitleText("本单位公告");
            loginName = "No";
        }else if (Contanst.annnouncement.type_Ji.equals(type)){
            setTitleText("集团公告");
            loginName = "?";
        }else if (Contanst.annnouncement.the_news.equals(type)){
            setTitleText("新闻");
            showLoading();
            mPresenter.getTheNews(pageNum,loginName);
            return;
        }
        showLoading();
        mPresenter.getAnnouncement(pageNum,loginName);
    }

    @Override
    public void getListSuccess(List<Announcement> list) {

        if (pageNum == 1){
            mList.clear();
        }
        mList.addAll(list);
        adapter.notifyDataSetChanged();

    }

    @Override
    public void onNetFinish(String tag, String startMsg) {
        super.onNetFinish(tag, startMsg);
        hideLoading();
        if (refreshLayout != null){
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
        }
    }


}
